/* Wrapper.cpp
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Function definitions for the Wrapper class.
*/

#include "Wrapper.h"

#include <algorithm>

using std::upper_bound;



Wrapper::Wrapper(const ViewProperties<text> &properties)
	: viewProperties(properties)
{
}



// Calculate line breaks in order to position the given cursor iterator
// on the given line of the display.
void Wrapper::Wrap(const text &t, text::const_iterator cursor, int cursorLine)
{
	lines.clear();

	// If the cursor line is less than zero, wrap to that number of lines
	// above the bottom of the view (so, -1 is the last visible line).
	cursorLine = AbsoluteLine(cursorLine);

	if(cursor != t.end() && *cursor == '\n')
		--cursor;

	// Until we have wrapped enough lines above the cursor, repeat:
	while(lines.size() <= static_cast<size_t>(cursorLine))
	{
		if(cursor == t.begin())
			lines.insert(lines.begin(), t.begin());
		else
		{
			// Assume that cursor is pointing to something right before a newline.
			text::const_iterator start = t.rfind('\n', cursor);
			text::const_iterator it = start;
			// If we found a newline, the paragraph starts immediately after the
			// newline character. Otherwise, it starts at the beginning of the text.
			if(start == t.end())
			{
				start = t.begin();
				it = start;
			}
			else
			{
				++it;
				if(cursor != t.end())
					++cursor;
			}

			// Form the line wrapping for this paragraph in a separate vector.
			static vector<text::const_iterator> paragraph;
			while(it <= cursor)
			{
				paragraph.push_back(it);
				it = wrap(it, t.end(), viewProperties.measure, viewProperties.lineWidth);
			}
			// Start at the beginning of this paragraph, next time.
			cursor = start;
			// Skip the '\n' character that the cursor is currently on.
			if(cursor != t.begin())
				--cursor;

			lines.insert(lines.begin(), paragraph.begin(), paragraph.end());
			// Clear the paragraph for use in the next time around.
			paragraph.clear();
		}
	}
	// Resize the lines to contain at most one line break for each
	// line up to and including the cursor line.
	int extraLines = lines.size() - (cursorLine + 1);
	if(extraLines)
		lines.erase(lines.begin(), lines.begin() + extraLines);

	// Now, continue wrapping until we reach the total number of lines.
	while(static_cast<int>(lines.size()) < viewProperties.lineCount + 1)
		lines.push_back(wrap(lines.back(), t.end(), viewProperties.measure, viewProperties.lineWidth));
}



// Get the start of the given line of the display.
// The end of each line is the start of the next.
text::const_iterator Wrapper::operator[](int line) const
{
	return lines[AbsoluteLine(line)];
}



// Get the line index of the given iterator.
int Wrapper::LineOf(text::const_iterator it) const
{
	return (upper_bound(lines.begin(), lines.end(), it) - lines.begin()) - 1;
}



// Get the position, within its line, of the given iterator.
int Wrapper::PositionOf(text::const_iterator it) const
{
	int line = LineOf(it);
	if(line == viewProperties.lineCount || line < 0)
		return 0;
	return PositionOf(it, line);
}



// Get the position of the given iterator within the given line.
// This result is only valid if the iterator is, indeed, on that line.
int Wrapper::PositionOf(text::const_iterator it, int line) const
{
	return viewProperties.measure((*this)[line], it);
}



// Map a line index, which may be relative to the bottom, to an
// absolute line index.
int Wrapper::AbsoluteLine(int line) const
{
	return line + (line < 0) * viewProperties.lineCount;
}



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */

